"""
斐波那契数列 1  1  2  3  5  8  13 。。。。

"""
# 编写函数实现斐波那契数列
# def fib(count):
#     index = 0
#     x, y = 0, 1
#     while index < count:
#         # 函数实现
#         # print(y)
#         #生成器实现
#         yield y
#         x, y = y, x + y
#         index += 1
#
# g = fib(5)
# print(type(g))
#
# for i in g:
#     print(i)

# for循环遍历generator时，拿不到generator的return与的返回值
# 如果想拿返回值，必须捕获StopIteration异常，返回包含在错误对象的value属性中
def fib(count):
    index = 0
    x, y = 0, 1
    while index < count:
        # 函数实现
        # print(y)
        #生成器实现
        yield y
        x, y = y, x + y
        index += 1
    return "lifeibiao is a good man"

g = fib(5)
print(type(g))

while 1:
    try:
        ret = next(g)
        print(ret)
    except StopIteration as e:
        print("返回值：",e.value)
        break